package com.sc.datacollect.util;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateFormatUtils {

	private static String format1 ="yyyy-MM-dd";
	private static String format2 ="yyyy-MM-dd HH:mm:ss";
	private static String format3 ="yyyyMMddHHmmss";
	
	/**
	 * 
	 * 函数名：getTimestampId
	 * 功能描述：根据给定日期生成具有3位毫秒的17位id，例：20121017094953953
	 * @param date
	 * @return 	（20121017094953953）@type String
	 * 异常： 
	 * @创建人：Guowm
	 * 创建时间：2012-10-17 上午09:52:38
	 * @修改人：Guowm
	 * 修改时间：2012-10-17 上午09:52:38
	 * 修改原因描述：
	 */
	public static String getTimestampId(java.util.Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(format3);
		Timestamp time = new Timestamp(date.getTime());
		String mantissa;
		int nanos = time.getNanos();
		if(nanos>99999999){
			mantissa = String.valueOf(time.getNanos()).substring(0,3);
		}else if(nanos>9999999){
			mantissa = "0"+String.valueOf(time.getNanos()).substring(0,2);
		}else if(nanos>999999){
			mantissa = "00"+String.valueOf(time.getNanos()).substring(0,1);
		}else{
			mantissa ="000";
		}
		return sdf.format(time)+mantissa;
	}
	
	/**
	 * 
	 * 函数名：getTimestampId
	 * 功能描述：根据当前日期生成具有3位毫秒的17位id，例：20121017094953953
	 * @return 		@type String
	 * 异常： 
	 * @创建人：Guowm
	 * 创建时间：2012-10-17 上午10:06:55
	 * @修改人：Guowm
	 * 修改时间：2012-10-17 上午10:06:55
	 * 修改原因描述：
	 */
	public static String getTimestampId(){
		SimpleDateFormat sdf = new SimpleDateFormat(format3);
		Timestamp time = new Timestamp(new java.util.Date().getTime());
		String mantissa;
		int nanos = time.getNanos();
		if(nanos>99999999){
			mantissa = String.valueOf(time.getNanos()).substring(0,3);
		}else if(nanos>9999999){
			mantissa = "0"+String.valueOf(time.getNanos()).substring(0,2);
		}else if(nanos>999999){
			mantissa = "00"+String.valueOf(time.getNanos()).substring(0,1);
		}else{
			mantissa ="000";
		}
		return sdf.format(time)+mantissa;
	}
	
	/**
	 * 
	 * 函数名：getTimestampId
	 * 功能描述：根据毫秒数
	 * @param t
	 * @return 		@type String
	 * 异常： 
	 * @创建人：Guowm
	 * 创建时间：2012-10-17 下午04:16:28
	 * @修改人：Guowm
	 * 修改时间：2012-10-17 下午04:16:28
	 * 修改原因描述：
	 */
	public static String getTimestampId(long t){
		SimpleDateFormat sdf = new SimpleDateFormat(format3);
		Timestamp time = new Timestamp(t);
		String mantissa;
		int nanos = time.getNanos();
		if(nanos>99999999){
			mantissa = String.valueOf(time.getNanos()).substring(0,3);
		}else if(nanos>9999999){
			mantissa = "0"+String.valueOf(time.getNanos()).substring(0,2);
		}else if(nanos>999999){
			mantissa = "00"+String.valueOf(time.getNanos()).substring(0,1);
		}else{
			mantissa ="000";
		}
		return sdf.format(time)+mantissa;
	}
	
	/**
	 * 
	 * 函数名：getTimestampId
	 * 功能描述：根据timestamp类型
	 * @param time
	 * @return 		@type String
	 * 异常： 
	 * @创建人：Guowm
	 * 创建时间：2012-10-17 下午04:16:48
	 * @修改人：Guowm
	 * 修改时间：2012-10-17 下午04:16:48
	 * 修改原因描述：
	 */
	public static String getTimestampId(Timestamp time){
		SimpleDateFormat sdf = new SimpleDateFormat(format3);
		String mantissa;
		int nanos = time.getNanos();
		if(nanos>99999999){
			mantissa = String.valueOf(time.getNanos()).substring(0,3);
		}else if(nanos>9999999){
			mantissa = "0"+String.valueOf(time.getNanos()).substring(0,2);
		}else if(nanos>999999){
			mantissa = "00"+String.valueOf(time.getNanos()).substring(0,1);
		}else{
			mantissa ="000";
		}
		return sdf.format(time)+mantissa;
	}
	
	/**
	 * get a sqldate with stringdate
	 * @param date stringdate
	 * @return
	 */
	public static Date getDate_sql(String date) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat(format1);
	    Date da=null;
		try {
			da = new Date(sdf.parse(date).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println("日期："+date+"转化失败！");
			da = new Date(0);
			e.printStackTrace();
		}	
		return da;
	}

	/**
	 * get a utildate with stringdate
	 * @param date string date
	 * @return util date
	 */
	public static java.util.Date getDate_util(String date) {
		// TODO Auto-generated method stub
		return getDate_util(date,format2);
	}
	
	
	public static java.util.Date getDate_util(String date,String format) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat(format);
	    java.util.Date da=null;
	    try {
			da = sdf.parse(date);
		//	da = (java.util.Date) Timestamp.valueOf("2008-06-08 00:00:00");
		//	System.out.println(da);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println("日期："+date+"转化失败！");
			da = new java.util.Date(0);
			e.printStackTrace();
		}
		return da;
	}
	
	/**
	 * get a string of currentdate. format:yyyyMMddhhmmss
	 * @return
	 */
	public static String getID(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		return sdf.format(new java.util.Date());
	}
	
	/**
	 * get a string date with utilDate
	 * @param date
	 * @param format:int 1.yyyy-MM-dd,2.yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String getDate_str(java.util.Date date,int format){
		SimpleDateFormat sdf = null;
		if(format==1){
			sdf = new SimpleDateFormat(format1);
		}else{
			sdf = new SimpleDateFormat(format2);
		}
		return sdf.format(date);
	}
	
	/**
	 * get a string date with utilDate and format
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDate_str(java.util.Date date,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	/**
	 * get a stringdate of yyyy-MM-dd with utilDate
	 * @param date
	 * @return
	 */
	public static String getDate_str(java.util.Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(format1);
		return sdf.format(date);
	}

	/**
	 * 判断是否是日期的格式
	 * 2017-07-31 08:32:00
	 * @param timeStr
	 * @return
	 */
	public static boolean isDateFormat(String timeStr) {
		String regex = "\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}";
		// 编译正则表达式
		Pattern pattern = Pattern.compile(regex);
		// 忽略大小写的写法
		// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(timeStr);
		return matcher.matches();
	}
	/**
	 * get a sqlDate with utilDate
	 * @param date sqlDate
	 * @return
	 */
	public static Date getDate_sql(java.util.Date date){
		return new Date(date.getTime());
	}

	/**
	 * get a format of yyyy-MM-dd
	 * @return
	 */
	public static String getFormat1() {
		return format1;
	}
	/**
	 * get a format of yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String getFormat2() {
		return format2;
	}	
	
	public static void main(String[] args) {
		String time2 = "2018/05/07 23:00:00";
		String time3 = "2018-4-21 3:0:00";
		time2 = time2.replace("/", "-");
		
		String dateStr = DateFormatUtils.getDate_str(DateFormatUtils.getDate_util(time3), 2);
		dateStr = dateStr.substring(0, 13) + ":00:00";
		
		System.out.println(dateStr);
	}
}
